/**
 * 计算多边形面积
 * @param path  [{lat:,lng:}],[{lat:,lng:}],[{lat:,lng:}]
 */
export function calcPolygonArea(path: Array<any>) {
  const radius = 6371009;
  const len = path.length;
  if (len < 3) return 0;
  let total = 0;
  const prev = path[len - 1];
  let prevTanLat = Math.tan((Math.PI / 2 - (prev[1] / 180) * Math.PI) / 2);
  let prevLng = (prev[0] / 180) * Math.PI;
  for (const i in path) {
    const tanLat = Math.tan((Math.PI / 2 - (path[i][1] / 180) * Math.PI) / 2);
    const lng = (path[i][0] / 180) * Math.PI;
    total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
    prevTanLat = tanLat;
    prevLng = lng;
  }
  return Math.abs(total * (radius * radius));
}
function polarTriangleArea(
  tan1: number,
  lng1: number,
  tan2: number,
  lng2: number
) {
  const deltaLng = lng1 - lng2;
  const t = tan1 * tan2;
  return 2 * Math.atan2(t * Math.sin(deltaLng), 1 + t * Math.cos(deltaLng));
}
